package bean;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;


//@WebServlet("/ServletDBPool")
public class ServletDBPool extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private Connection conn;   
 
    public ServletDBPool() {
        try {
        	Context context = new InitialContext();
        	DataSource data =(DataSource) context.lookup("java:comp/env/jdbc/OracleDB");
        	conn = data.getConnection();
        	System.out.println("success pool");
		} catch (Exception e) {
			e.printStackTrace();
		}
       
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ArrayList<MemberVo> al = new ArrayList<>();
		String sql= "select * from member";
		try {
			PreparedStatement pst = conn.prepareStatement(sql);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				MemberVo vo = new MemberVo();
				vo.setMid(rs.getString("mid"));
				vo.setIrum(rs.getString("irum"));
				al.add(vo);
			}
			
			request.setAttribute("al", al);
			ServletContext servletContext = getServletContext();
			RequestDispatcher dispatcher = servletContext.getRequestDispatcher("/servlet/servlet_pool.jsp");
			dispatcher.forward(request, response);
		
		} catch (Exception e) {
				e.printStackTrace();
				
		}
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}
